//
//  LFReportDetailController.m
//  ServiceBaZhou
//
//  Created by 刘江涛 on 2017/8/24.
//  Copyright © 2017年 weierda. All rights reserved.
//

#import "LFReportDetailController.h"

#import "LFPartnerCell.h"

#import "ChangeInforCell.h"

#import "WebSiteModel.h"

#import "StockChangeModel.h"

#import "InvestInforModel.h"

#import "PartnerModel.h"

#define UpImage [UIImage imageNamed:@"up"]

#define DownImage [UIImage imageNamed:@"icon_down"]

@interface LFReportDetailController ()<UITableViewDelegate,UITableViewDataSource>

@property (nonatomic,strong) UITableView *tableView;

@property (nonatomic,strong) NSArray *baseDataArray;

@property (nonatomic,strong) NSArray *baseDataArray2;

@property (nonatomic,strong) NSArray *titleArray;

@property (nonatomic,strong) NSArray *assestArray;

@property (nonatomic,strong) NSArray *assestArray2;

@property (nonatomic,strong) NSMutableArray *webSiteArray;

@property (nonatomic,strong) NSMutableArray *stockChangeArray;

@property (nonatomic,strong) NSMutableArray *investArray;

@property (nonatomic,strong) NSMutableArray *partnerArray;

@property (nonatomic,strong) NSMutableArray *closeArray;

@property (nonatomic,strong) NSArray *headNumArray;

@end

@implementation LFReportDetailController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    [self createNavigation:@"2015年度报告"];
    
    _baseDataArray = @[@"注册号",@"企业经营状态",@"从业人数",@"本年度是否发生股东股权转让",@"企业是否有投资信息或购买其他公司股权",@"企业联系电话",@"电子邮箱",@"企业通讯地址"];
    
    NSDictionary *baseDic = _model.BasicInfoData;
    
    _baseDataArray2 = @[baseDic[@"RegNo"],baseDic[@"Status"],baseDic[@"EmployeeCount"],baseDic[@"IsStockRightTransfer"],baseDic[@"HasNewStockOrByStock"],baseDic[@"ContactNo"],baseDic[@"EmailAddress"],baseDic[@"Address"]];
    
    _titleArray = @[@"基本信息",@"股东（发起人）及出资信息",@"企业资产状况信息",@"对外投资信息",@"股权变更信息",@"网址信息"];
    
    _assestArray = @[@"资产总额",@"所有者权益合计",@"营业总收入",@"营业总收入中主营业务",@"利润总额",@"净利润",@"纳税总额",@"负债总额"];
    
    NSDictionary *assestDic = _model.AssetsData;
    _assestArray2 = @[assestDic[@"TotalAssets"],assestDic[@"TotalOwnersEquity"],assestDic[@"GrossTradingIncome"],assestDic[@"MainBusinessIncome"],assestDic[@"TotalProfit"],assestDic[@"NetProfit"],assestDic[@"TotalTaxAmount"],assestDic[@"TotalLiabilities"]];
    
    //网址数据
    NSArray *webArray = _model.WebSiteList;
    
    JSONModelArray *webSiteModelArray = [[JSONModelArray alloc]initWithArray:webArray modelClass:[WebSiteModel class]];
    
    self.webSiteArray = [NSMutableArray array];
    
    [self.webSiteArray addObjectsFromArray:(NSArray *)webSiteModelArray];
    
    //股权变更数据
    NSArray *stockArray = _model.StockChangeList;
    
    JSONModelArray *stockModelArray = [[JSONModelArray alloc]initWithArray:stockArray modelClass:[StockChangeModel class]];
    
    self.stockChangeArray = [NSMutableArray array];
    
    [self.webSiteArray addObjectsFromArray:(NSArray *)stockModelArray];
    
    //对外投资
    NSArray *inveArray = _model.InvestInfoList;
    
    JSONModelArray *InvestInforModelArray = [[JSONModelArray alloc]initWithArray:inveArray modelClass:[InvestInforModel class]];
    
    self.investArray = [NSMutableArray array];
    
    [self.webSiteArray addObjectsFromArray:(NSArray *)InvestInforModelArray];
    
    //股东
    NSArray *partnArray = _model.PartnerList;
    
    JSONModelArray *partnerModelArray = [[JSONModelArray alloc]initWithArray:partnArray modelClass:[PartnerModel class]];
    
    self.partnerArray = [NSMutableArray array];
    
    [self.partnerArray addObjectsFromArray:(NSArray *)partnerModelArray];
    
    _headNumArray = @[@"",[NSString stringWithFormat:@"%ld",partnArray.count],@"",[NSString stringWithFormat:@"%ld",inveArray.count],[NSString stringWithFormat:@"%ld",_stockChangeArray.count],[NSString stringWithFormat:@"%ld",webArray.count]];
    
    _closeArray = [NSMutableArray arrayWithCapacity:0];
    for (NSInteger i = 0; i < 6; i ++) {
        [_closeArray addObject:[NSNumber numberWithBool:YES]];
    }
    
    [self createTableView];
    
}

- (void)createTableView{
    _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 64, Width, Height - 64) style:UITableViewStylePlain];
    _tableView.delegate = self;
    _tableView.dataSource = self;
    
    [_tableView registerNib:[UINib nibWithNibName:@"LFPartnerCell" bundle:nil] forCellReuseIdentifier:@"LFPartnerCell"];
    [_tableView registerNib:[UINib nibWithNibName:@"ChangeInforCell" bundle:nil] forCellReuseIdentifier:@"ChangeInforCell"];
    
    [self.view addSubview:_tableView];
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 6;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    if ([_closeArray[section] intValue]) {
        return 0;
    }
    
    switch (section) {
        case 0:
            //
            return _baseDataArray.count;
            break;
            
        case 1:
            //
            return _partnerArray.count;
            break;
            
        case 2:
            //
            return _assestArray.count;
            break;
            
        case 3:
            //
            return _investArray.count;
            break;
            
        case 4:
            //
            return _stockChangeArray.count;
            break;
            
            
        default:
            return _webSiteArray.count;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    switch (indexPath.section) {
        case 0:
            //
            return 40;
            break;
            
        case 1:
            //
            return 141;
            break;
            
        case 2:
            //
            return 40;
            break;
            
        case 3:
            //
            return 40;
            break;
            
        case 4:
            //
            return 172;
            break;
            
        default:
            return 40;
            break;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 10;
}

- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 40;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    if(indexPath.section != 0 && indexPath.section != 2){
        tableView.separatorStyle = NO;
    }else{
        tableView.separatorStyle = YES;
    }
    
    if(indexPath.section == 0){
        static NSString *iden1 = @"iden1";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden1];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden1];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = _baseDataArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:12.0];
        cell.detailTextLabel.text = _baseDataArray2[indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
        cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#696969"];
        return cell;
    }else if (indexPath.section == 1){
        
        PartnerModel *model = _partnerArray[indexPath.row];
        
        LFPartnerCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LFPartnerCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.nameLabel.text = model.Name;
        cell.shouldTimeLabel.text = [model.ShouldDate substringToIndex:10];
        cell.shouldMoneyLabel.text = model.ShouldCapi;
        cell.shouldTypeLabel.text = model.ShouldType;
        cell.realMoneyLabel.text = model.RealCapi;
        cell.realTimeLabel.text = [model.RealDate substringToIndex:10];
        cell.realTypeLabel.text = model.RealType;
        
        return cell;
    }else if (indexPath.section == 2){
        static NSString *iden2 = @"iden2";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden2];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:iden2];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = _assestArray[indexPath.row];
        cell.textLabel.font = [UIFont systemFontOfSize:12.0];
        cell.detailTextLabel.text = _assestArray2[indexPath.row];
        cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
        cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#696969"];
        return cell;
        
    }else if (indexPath.section == 3){
        
        InvestInforModel *model = _investArray[indexPath.row];
        
        static NSString *iden3 = @"iden3";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden3];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:iden3];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = model.Name;
        cell.textLabel.font = [UIFont systemFontOfSize:12.0];
        cell.textLabel.textColor = [UIColor colorWithRed:0/255.0 green:122/255.0 blue:255/255.0 alpha:1.0];
        cell.detailTextLabel.text = model.RegNo;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
        cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#696969"];
        return cell;
    }else if (indexPath.section == 4){
        
        StockChangeModel *model = _stockChangeArray[indexPath.row];
        
        ChangeInforCell *cell = [tableView dequeueReusableCellWithIdentifier:@"ChangeInforCell"];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        
        cell.numberLabel.text = [NSString stringWithFormat:@"%ld",indexPath.row + 1];
        cell.myTitleLabel.text = model.Name;
        cell.beforeLabel.text = model.Before;
        cell.afterLabel.text = model.After;
        return cell;
    }else{
        
        WebSiteModel *model = _webSiteArray[indexPath.row];
        
        static NSString *iden4 = @"iden4";
        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:iden4];
        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleSubtitle reuseIdentifier:iden4];
            cell.selectionStyle = UITableViewCellSelectionStyleNone;
        }
        cell.textLabel.text = model.Name;
        cell.textLabel.font = [UIFont systemFontOfSize:12.0];
        cell.detailTextLabel.text = model.WebSite;
        cell.detailTextLabel.font = [UIFont systemFontOfSize:10];
        cell.detailTextLabel.textColor = [UIColor colorWithHexString:@"#696969"];
        return cell;
    }

}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    UIButton *headView = [[UIButton alloc]initWithFrame:CGRectMake(0, 0, Width, 40)];
    headView.backgroundColor = [UIColor whiteColor];
    [headView addTarget:self action:@selector(headClick:) forControlEvents:UIControlEventTouchUpInside];
    headView.tag = 2000 + section;
    
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(20, 0, Width - 100, 40)];
    label.text = _titleArray[section];
    label.font = [UIFont boldSystemFontOfSize:14];
    label.textAlignment = NSTextAlignmentLeft;
    [headView addSubview:label];
    
    UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(Width - 40, 8, 24, 24)];
    if([_closeArray[section] intValue]){
        imageView.image = UpImage;
    }else{
        imageView.image = DownImage;
    }
    
    
    UILabel *numLabel = [[UILabel alloc]initWithFrame:CGRectMake(Width - 80, 0, 40, 40)];
    numLabel.text = _headNumArray[section];
    numLabel.font = [UIFont systemFontOfSize:14];
    numLabel.textAlignment = NSTextAlignmentRight;
    numLabel.textColor = [UIColor colorWithHexString:@"#A1A1A1"];
    
    UILabel *noLabel = [[UILabel alloc]initWithFrame:CGRectMake(Width - 100, 0, 80, 40)];
    noLabel.text = @"无";
    noLabel.font = [UIFont systemFontOfSize:14];
    noLabel.textAlignment = NSTextAlignmentRight;
    noLabel.textColor = [UIColor colorWithHexString:@"#A1A1A1"];
    
    if((_partnerArray.count == 0 && section == 1) || (_investArray.count == 0 && section == 3) || (_stockChangeArray.count == 0 && section == 4) || (_webSiteArray.count == 0 && section == 5) ){
        
        [headView addSubview:noLabel];
        
    }else{
        if(section == 1 || section > 2){
            [headView addSubview:numLabel];
        }
        [headView addSubview:imageView];
    }
    
    return headView;
}

- (void)headClick:(UIButton *)button{
    BOOL isClose = [[_closeArray objectAtIndex:button.tag - 2000] boolValue];
    [_closeArray replaceObjectAtIndex:button.tag - 2000 withObject:[NSNumber numberWithBool:!isClose]];
    
    //NSIndexSet:数字的集合
    //只刷新对应的段的数据
    [_tableView reloadSections:[NSIndexSet indexSetWithIndex:button.tag - 2000] withRowAnimation:UITableViewRowAnimationAutomatic];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
